package com.fourmob.datetimepicker.date;

import com.fourmob.datetimepicker.ResourceTable;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import java.io.IOException;

public class TextViewWithCircularIndicator extends Text implements Component.DrawTask {
	private static HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x000110, "DatePickerDialog");
	private static HiLogLabel label1 = new HiLogLabel(HiLog.LOG_APP, 0x000110, "DatePickerDialog3");

	private int mCircleColor;
    private Paint mCirclePaint = new Paint();
    private Paint mTextPaint = new Paint();
	private boolean mDrawCircle;
	private final String mItemIsSelectedText;
	private final int mRadius;
	private String needtext;

	public TextViewWithCircularIndicator(Context context,AttrSet attrSet)
		throws NotExistException, WrongTypeException, IOException {
		super(context,attrSet);
		mCircleColor = Color.getIntColor("#c5e4ef");
		mRadius = 60;
		mItemIsSelectedText = "mItemIsSelectedText";
		init();
	}

	@Override
	public void setText(String text) {
		super.setText(text);
		HiLog.info(label,"TextView_setText " + text);
		needtext = text;
		if (needtext != null && needtext.length() > 0) {
			invalidate();
		}
	}

	private void init() {
		mCirclePaint.setFakeBoldText(true);
		mCirclePaint.setAntiAlias(true);
		mCirclePaint.setColor(new Color(mCircleColor));
		mCirclePaint.setTextAlign(TextAlignment.CENTER);
		mCirclePaint.setStyle(Paint.Style.FILL_STYLE);
		mCirclePaint.setAlpha(60);
		addDrawTask(this::onDraw);
	}

	public void drawIndicator(boolean drawIndicator) {
		mDrawCircle = drawIndicator;
		invalidate();
	}

	public CharSequence getContentDescription() {
		HiLog.info(label,"TextViewWithCircularIndicator00");
		CharSequence text = needtext;
		HiLog.info(label,"TextViewWithCircularIndicator01 " + text);
		if (mDrawCircle) {
			HiLog.info(label,"TextViewWithCircularIndicator02 " + text);
			/*text = String.format(mItemIsSelectedText, text);*/
			HiLog.info(label,"TextViewWithCircularIndicator03 " + text);
        }
		HiLog.info(label,"TextViewWithCircularIndicator04 " + text);
		return text;
	}

	@Override
	public void onDraw(Component component, Canvas canvas) {
		HiLog.info(label,"TextView_Circular");
		if (mDrawCircle) {
			HiLog.info(label,"TextView_Circular1");
			int width = getWidth();
			int heigth = getHeight();
			int radius = Math.min(width, heigth) / 2;
			mCirclePaint.setColor(new Color(Color.getIntColor("#c5e4ef")));
			canvas.drawCircle(width / (float)2, heigth / (float)2, radius, mCirclePaint);
            if (needtext != null && needtext.length() > 0){
            	mTextPaint.setColor(new Color(Color.getIntColor("#9a9a9a")));
            	mTextPaint.setTextSize(radius / 3 * 2 + 5);
            	canvas.drawText(mTextPaint,needtext,width / (float)2 - radius / (float)4 * 3,heigth / (float)2 + radius / (float)4);
			}
		}
	}
}